package com.xbts.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.PartyActiveManage;
import com.xbts.system.mapper.PartyActiveManageMapper;
import com.xbts.system.service.IPartyActiveManageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 活动管理Service业务层处理
 *
 * @author xbts
 * @date 2023-06-12
 */
@Service
public class PartyActiveManageServiceImpl extends ServiceImpl<PartyActiveManageMapper, PartyActiveManage> implements IPartyActiveManageService {

    /**
     * 查询活动管理列表
     *
     * @param partyActiveManage 活动管理
     * @return 活动管理
     */
    @Override
    public List<PartyActiveManage> selectPartyActiveManageList(PartyActiveManage partyActiveManage) {
        QueryWrapper<PartyActiveManage> queryWrapper = partyActiveManage.queryWrapper();

        // 活动创建时间筛选
        Map<String, Object> params = partyActiveManage.getParams();
        if (Objects.nonNull(params)) {
            // 活动创建时间筛选
            if (params.containsKey("startTime") && StringUtils.isNotBlank(params.get("startTime").toString())) {
                queryWrapper.ge("create_time", params.get("startTime").toString());
            }
            // 活动结束时间筛选
            if (params.containsKey("endTime") && StringUtils.isNotBlank(params.get("endTime").toString())) {
                queryWrapper.le("create_time", params.get("endTime").toString());
            }
        }
        queryWrapper.orderByDesc("create_time");
        return list(queryWrapper);
    }

    /**
     * 查询活动管理
     *
     * @param activityCode 活动管理主键
     * @return 活动管理
     */
    @Override
    public PartyActiveManage selectPartyActiveManageByActivityCode(String activityCode) {
        return getById(activityCode);
    }

    /**
     * 新增活动管理
     *
     * @param partyActiveManage 活动管理
     * @return 结果
     */
    @Override
    public boolean insertPartyActiveManage(PartyActiveManage partyActiveManage) {
        // 判断活动名称是否存在
        QueryWrapper<PartyActiveManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_code", partyActiveManage.getActivityCode());
        List<PartyActiveManage> activeManageList = list(queryWrapper);
        if (activeManageList.size() > 0) {
            throw new ServiceException("活动编号已存在，请重新输入！");
        }

        partyActiveManage.setCreateBy(SecurityUtils.getUserId().toString());
        partyActiveManage.setCreateTime(DateUtils.getNowDate());
        return save(partyActiveManage);
    }

    /**
     * 修改活动管理
     *
     * @param partyActiveManage 活动管理
     * @return 结果
     */
    @Override
    public boolean updatePartyActiveManage(PartyActiveManage partyActiveManage) {
//        // 判断活动名称是否存在
//        QueryWrapper<PartyActiveManage> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("activity_name", partyActiveManage.getActivityName());
//        queryWrapper.ne("activity_code", partyActiveManage.getActivityCode());
//        List<PartyActiveManage> activeManageList = list(queryWrapper);
//        if (activeManageList.size() > 0) {
//            throw new ServiceException("活动名称已存在");
//        }

        partyActiveManage.setUpdateBy(SecurityUtils.getUserId().toString());
        partyActiveManage.setUpdateTime(DateUtils.getNowDate());
        return updateById(partyActiveManage);
    }

    /**
     * 批量删除活动管理
     *
     * @param activityCodes 需要删除的活动管理主键
     * @return 结果
     */
    @Override
    public boolean deletePartyActiveManageByActivityCodes(List<String> activityCodes) {
        return removeBatchByIds(activityCodes);
    }

    /**
     * 删除活动管理信息
     *
     * @param activityCode 活动管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deletePartyActiveManageByActivityCode(String activityCode) {
        return removeById(activityCode);
    }

}
